package com.xixi.canal.client.support;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * DML操作转换对象
 *
 * @author rewerma 2018-8-19 下午11:30:49
 * @version 1.0.0
 */
public class Dml implements Serializable {

    private static final long         serialVersionUID = 2611556444074013268L;

    // topic
    private String destination;
    private Long batchId;
    // db
    private String database;
    private String table;
    private String eventType;

    private Boolean isDdl;
    private String sql;

    // binlog 执行耗时
    private Long executeTime;
    // 同步时间
    private Long syncTime;

    private Long delayTime;

    private List<String> pkName;

    private List<RowData> data;

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public Long getBatchId() {
        return batchId;
    }

    public void setBatchId(Long batchId) {
        this.batchId = batchId;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getEventType() {
        return eventType;
    }

    public void setEventType(String eventType) {
        this.eventType = eventType;
    }

    public Boolean getIsDdl() {
        return isDdl;
    }

    public void setIsDdl(Boolean ddl) {
        isDdl = ddl;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public Long getExecuteTime() {
        return executeTime;
    }

    public void setExecuteTime(Long executeTime) {
        this.executeTime = executeTime;
    }

    public Long getSyncTime() {
        return syncTime;
    }

    public void setSyncTime(Long syncTime) {
        this.syncTime = syncTime;
    }

    public Long getDelayTime() {
        return delayTime;
    }

    public void setDelayTime(Long delayTime) {
        this.delayTime = delayTime;
    }

    public List<RowData> getData() {
        return data;
    }

    public void setData(List<RowData> data) {
        this.data = data;
    }

    public void addRowData(RowData row) {
        if (null == data) data = new ArrayList<>();
        data.add(row);
    }

    public List<String> getPkName() {
        return pkName;
    }

    public void setPkName(List<String> pkName) {
        this.pkName = pkName;
    }

    @Override
    public String toString() {
        return "Dml{" +
                "destination='" + destination + '\'' +
                ", batchId=" + batchId +
                ", database='" + database + '\'' +
                ", table='" + table + '\'' +
                ", eventType='" + eventType + '\'' +
                ", isDdl=" + isDdl +
                ", sql='" + sql + '\'' +
                ", executeTime=" + executeTime +
                ", syncTime=" + syncTime +
                ", delayTime=" + delayTime +
                ", pkName=" + pkName +
                ", data=" + data +
                '}';
    }

    public static class RowData {

        private List<ColumnData> columns;
        private Set<String> keyColumns;

        public void addKeyColumn(String columnName) {
            if (null == keyColumns) keyColumns = new HashSet<>();
            keyColumns.add(columnName);
        }

        public void addColumn(ColumnData column) {
            if (null == columns) columns = new ArrayList<>();
            columns.add(column);
        }

        public List<ColumnData> getColumns() {
            return columns;
        }

        public void setColumns(List<ColumnData> columns) {
            this.columns = columns;
        }

        public Set<String> getKeyColumns() {
            return keyColumns;
        }

        public void setKeyColumns(Set<String> keyColumns) {
            this.keyColumns = keyColumns;
        }

        @Override
        public String toString() {
            return "RowData{" +
                    "columns=" + columns +
                    ", keyColumns=" + keyColumns +
                    '}';
        }
    }

    public static class ColumnData {
        private String name;

        private Boolean primaryKey;

        private String sqlType;

        private Boolean updated;

        private Object value;

        private Object oldValue;

        private Boolean isNull;

        public Boolean getPrimaryKey() {
            return primaryKey;
        }

        public void setPrimaryKey(Boolean primaryKey) {
            this.primaryKey = primaryKey;
        }

        public Boolean getUpdated() {
            return updated;
        }

        public void setUpdated(Boolean updated) {
            this.updated = updated;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSqlType() {
            return sqlType;
        }

        public void setSqlType(String sqlType) {
            this.sqlType = sqlType;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public Object getOldValue() {
            return oldValue;
        }

        public void setOldValue(Object oldValue) {
            this.oldValue = oldValue;
        }

        public Boolean getIsNull() {
            return isNull;
        }

        public void setIsNull(Boolean aNull) {
            isNull = aNull;
        }

        @Override
        public String toString() {
            return "ColumnData{" +
                    "name='" + name + '\'' +
                    ", sqlType='" + sqlType + '\'' +
                    ", value=" + value +
                    ", oldValue=" + oldValue +
                    ", isNull=" + isNull +
                    '}';
        }
    }

}
